package com.hbu.product.hbumallproduct.controller;

import com.hbu.common.utils.R;
import com.hbu.product.hbumallproduct.entity.CategoryEntity;
import com.hbu.product.hbumallproduct.service.CategoryService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


/**
 * 菜单的controller
 *
 * @author zhaoxiaohan
 * @email 13315217827@163.com
 * @date 2022-10-27 22:52:06
 */
@RestController
@RequestMapping("hbumallproduct/category")
@AllArgsConstructor
public class CategoryController {

    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping(value = "/list/tree", method = RequestMethod.GET)
    public R list() {
        return R.ok().put("data", categoryService.listWithTree());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        return R.ok().put("data", categoryService.getById(catId));
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public R save(@RequestBody CategoryEntity category) {
        return R.result(categoryService.save(category),"保存");
    }

    /**
     * 批量修改
     */
    @RequestMapping(value = "/update/sort",method = RequestMethod.POST)
    public R updateSort(@RequestBody CategoryEntity[] categories) {
        return R.result(categoryService.updateBatchById(Arrays.asList(categories)),"修改");
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public R update(@RequestBody CategoryEntity category){
        categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestBody Long[] catIds) {
        return categoryService.removeMenusByIds(catIds);
    }

}
